package com.googlecode.pacswitch.conditions;

import java.nio.channels.AsynchronousSocketChannel;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;

import com.googlecode.pacswitch.Condition;
import com.googlecode.pacswitch.ObjectFactory;
import com.googlecode.pacswitch.PacSwitch;

/**
 * @author Andrey Pavlenko
 */
@XmlType(name = "Not", namespace = ObjectFactory.CNS)
public class Not implements Condition {
    private JAXBElement<Condition> _condition;

    @XmlAnyElement(lax = true)
    public JAXBElement<Condition> getCondition() {
        return _condition;
    }

    public void setCondition(final JAXBElement<Condition> condition) {
        _condition = condition;
    }

    @Override
    public boolean apply(final PacSwitch ps,
            final AsynchronousSocketChannel clientSocket) {
        final Condition c = getCondition().getValue();
        return (c == null) ? true : !c.apply(ps, clientSocket);
    }
}
